package com.stylefeng.guns.modular.system.controller.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.dao.ProjectTableMapper;
import com.stylefeng.guns.common.persistence.dao.ProjectTablePropertyMapper;
import com.stylefeng.guns.common.persistence.model.ProjectTable;
import com.stylefeng.guns.common.persistence.model.ProjectTableProperty;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.dao.ProjectTableDao;
import com.stylefeng.guns.modular.system.dao.ProjectTablePropertyDao;
import com.stylefeng.guns.modular.system.warpper.ProjectTablePropertyWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lmshe
 * @create 2018-07-04 14:12
 * @desc
 */
@Controller
@RequestMapping("/projectTableProperty")
public class ProjectTablePropertyController extends BaseController {
    private static String PREFIX = "/system/projectTableProperty/";

    @Resource
    private ProjectTablePropertyDao projectTablePropertyDao;

    @Resource
    private ProjectTablePropertyMapper projectTablePropertyMapper;

    @Resource
    private ProjectTableDao projectTableDao;

    @Resource
    private ProjectTableMapper projectTableMapper;
    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "project_table_property.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = projectTablePropertyDao.getProjectTablePropertyList(page, page.getOrderByField(), page.isAsc());
        page.setRecords((List<Map<String, Object>>) new ProjectTablePropertyWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/project_table_property_add")
    public String projectTablePropertyAdd(Model model) {
        List<ProjectTable> projectTableList = this.projectTableMapper.selectList(new EntityWrapper<>());
        model.addAttribute("projectTableList", projectTableList);
        return PREFIX + "project_table_property_add.html";
    }

    /**
     * 添加
     * @param model
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增虚拟表结构",key = "project_table_property")
    public Object add(ProjectTableProperty model) {
        model.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/project_table_property_update/{id}")
    public String projectTablePropertyUpdate(@PathVariable Integer id, Model model) {
        ProjectTableProperty projectTableProperty = this.projectTablePropertyMapper.selectById(id);
        model.addAttribute("projectTableProperty",projectTableProperty);
        List<ProjectTable> projectTableList = this.projectTableMapper.selectList(new EntityWrapper<>());
        model.addAttribute("projectTableList", projectTableList);
        LogObjectHolder.me().set(projectTableProperty);
        return PREFIX + "project_table_property_edit.html";
    }

    /**
     * 修改
     * @param model
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改虚拟表结构",key = "project_table_property")
    public Object update(ProjectTableProperty model) {
        this.projectTablePropertyMapper.updateById(model);
        return super.SUCCESS_TIP;
    }
}
